package com.skb.core.service;

import com.skb.core.dao.UserDao;
import com.skb.core.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Service("userService")
public class UserService implements IUserService {

    @Autowired
    private UserDao userDao;

    @Transactional
    @Override
    public User create(User o) {
        return userDao.create(o);
    }

    @Transactional
    @Override
    public User get(Serializable id) {
        return userDao.get(id);
    }

    @Transactional
    @Override
    public void delete(User o) {
        userDao.delete(o);
    }

    @Transactional
    @Override
    public User update(User o) {
        return userDao.update(o);
    }

    @Transactional(readOnly = true)
    @Override
    public User getUserDetails(String username) {
        User user = userDao.findByUserName(username);
        return new User(user.getUsername(), "", user.isEnabled(), user.getUserRole(), user.getUserAccount());
    }

    @Transactional(readOnly = true)
    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }

    @Override
    public User findByUserName(Serializable username) {
        return userDao.findByUserName(username);
    }
}
